package io.adrainty.bolee.security.satoken.register;

import cn.dev33.satoken.oauth2.granttype.handler.SaOAuth2GrantTypeHandlerInterface;
import io.adrainty.bolee.security.satoken.handler.BaseGrantTypeHandler;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>GrantTypeHandlerRegister</p>
 *
 * @author AdRainty
 * @version V1.0.0
 * @description GrantTypeHandlerRegister
 * @since 2025/8/6 15:46:19
 */

@Component
public class GrantTypeHandlerRegister {

    private final Map<String, SaOAuth2GrantTypeHandlerInterface> handlerInterfaceMap;

    public GrantTypeHandlerRegister(List<SaOAuth2GrantTypeHandlerInterface> handlerInterfaces) {
        this.handlerInterfaceMap = new HashMap<>(handlerInterfaces.size());
        for (SaOAuth2GrantTypeHandlerInterface handlerInterface: handlerInterfaces) {
            if (handlerInterface.getClass().getSuperclass().equals(BaseGrantTypeHandler.class)) {
                BaseGrantTypeHandler grantTypeHandler = (BaseGrantTypeHandler) handlerInterface;
                this.handlerInterfaceMap.put(grantTypeHandler.getActualGrantType(), handlerInterface);
            }
        }
    }

    /**
     * 获取处理器
     * @param grantType 授权方式
     * @return 处理器
     */
    public SaOAuth2GrantTypeHandlerInterface getHandlerInterface(String grantType) {
        return handlerInterfaceMap.get(grantType);
    }

}
